package timer;

import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import entity.Users;

/**
 * Created by wuzhao on 2017/4/5 0005.
 */
public class TimerTaskEntity {

    private static Map<Integer,List<Users>> tasks = new HashMap<Integer, List<Users>>();

    private static int cursor = 0;

    private static final int ticksPerWheel = 5;


    public static boolean addTask(int index,Users task){

        if (task == null)
            return false;
        List<Users> userss = tasks.get(index);
        if (userss == null){
            userss = new ArrayList<Users>();
            tasks.put(index,userss);
        }
        userss.add(task);
        return true;
    }

    private static void moveCursor(){
        if (cursor >= (ticksPerWheel-1)){
            cursor = -1;
        }
        cursor++;
    }

    public static List<Users> moveTask(){
        System.out.printf("==="+cursor);

        List<Users> taskList = tasks.get(cursor);
        List<Users> copyTask = new ArrayList<Users>();
        if (!CollectionUtils.isEmpty(taskList)){
            copyTask.addAll(taskList);
            taskList.clear();
        }
        moveCursor();
        return copyTask;
    }
}
